<?php
/**
 * Created by PhpStorm.
 * User: wei
 * Date: 2016/5/17
 * Time: 16:01
 */
namespace Admin\Controller;
use Think\Controller;

class CommentManageController extends CommonController{
    protected function _initialize(){
        parent::_initialize();
        $this->breadcrumb1='基础数据管理';
        $this->breadcrumb2='评价体系管理';
        $this->breadcrumb3='佣金与奖励设置';
    }

    public function manage(){
        $menu_id=$_GET['id'];
        $jur=jur_str($menu_id,$this->user_id);
        $data=M("pingjia")->where("status!=1")->select();
        $money=M('pingjia')->where("id=4")->find();
        $this->assign('data',$data);
        $this->assign('money',$money);
        $this->assign('update',$jur['is_update']);// 赋值数据集
        $this->assign('delete',$jur['is_delete']);// 赋值数据集
        $this->display();

    }

    public function update(){
        $con=I("post.con",'','int');
        $var=I("post.va",'','int');
        $id=I("post.id",'','int');
        $save['constant']=$con;
        $save['variable']=$var;
        M('pingjia')->where("id=$id")->save($save);
        exit;
    }

    public function updatemoney(){
        $money=I("post.money",'','int');
        $save['money']=$money;
        M('pingjia')->where("id=4")->save($save);
        exit;
    }

    public function change(){
        $id=I("post.id",'','int');
        $m=M("pingjia");
        $m->startTrans();
        $where['status']=2;
        $sa['status']=0;
        $reload=M("pingjia")->where($where)->save($sa);
        $save['status']=2;
        $change=M("pingjia")->where("id=$id")->save($save);
        if($reload && $change){
            $m->commit();//成功则提交
            echo 'yes';exit;
        }else{
            $m->rollback();//不成功，则回滚
            echo 'no';exit;
        }
    }

    //规则编辑
    public function commentList(){
        $menu_id=$_GET['id'];
        $jur=jur_str($menu_id,$this->user_id);
        $this->breadcrumb3='规则说明';
        $list=M('comment')->select();
        $arr=$this->generateTree($list);
        $this->assign('list',$arr);
        $this->assign('update',$jur['is_update']);// 赋值数据集
        $this->assign('delete',$jur['is_delete']);// 赋值数据集
        $this->display();
    }


    //数组分类
   private function generateTree($items){
        $tree = array();
        foreach($items as $item){
            if($item['p_id']==0){
                $tree[$item['id']][]=$item;
                foreach($items as $k=>$v){
                    if($v['p_id']==$item['id']){
                        $tree[$item['id']][]=$v;
                        unset($items[$k]);
                    }
                }
            }
        }
        return $tree;
    }

    //编辑
    public function commentEdit(){
        $id=I('get.id');
        $where['p_id']=$id;
        $list=M('comment')->where($where)->select();
        foreach($list as $k=>$v){
            switch($v['grade']){
                case 1:
                    $list[$k]['name']='一星';
                    break;
                case 2:
                    $list[$k]['name']='二星';
                    break;
                case 3:
                    $list[$k]['name']='三星';
                    break;
                case 4:
                    $list[$k]['name']='四星';
                    break;
                case 5:
                    $list[$k]['name']='五星';
                    break;
                case 6:
                    $list[$k]['name']='六星';
                    break;
            }
        }
        $this->assign('list',$list);
        $this->display();
    }


    //修改操作接口
    public function updatename(){
        $name=I('post.re');
        $id=I('post.id');
       foreach($name as $k=>$v){
           $where['id']=$id[$k];
           $save['c_name']=$v;
           M('comment')->where($where)->save($save);
       }
        echo 1;exit;
    }
}